package com.sxm.yiwei.utils;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.util.Log;

import java.math.BigDecimal;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.DecimalFormat;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Administrator on 2017/9/12.
 */

public class HandleDataUtil {
    /**
     * double数值太大时，不以科学计数法形式先生，保留小数点后两位
     */
    /*public static String saveToPoint2(double a1) {
        DecimalFormat df = new DecimalFormat("#,##0.00");// 格式化设置
        return df.format(a1 / 100.0);
    }*/
    /*public static String saveToPoint2(double a1) {
        DecimalFormat df = new DecimalFormat("#,##0.00");// 格式化设置
        return df.format(a1);
    }*/
    public static Double saveToPoint2(double a1) {
        double total_amount = 0.0;

        BigDecimal b = new BigDecimal(a1);
        total_amount = b.setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue();
        return total_amount;
    }
    public static String withoutChinese(String string){
        String reg = "[\u4e00-\u9fa5]";
        Pattern pat = Pattern.compile(reg);
        Matcher mat=pat.matcher(string);
        String repickStr = mat.replaceAll("");
            return repickStr;


    }
    public static String getString(String s, String s1)//s是需要删除某个子串的字符串s1是需要删除的子串
    {
        int postion = s.indexOf(s1);
        int length = s1.length();
        int Length = s.length();
        String newString = s.substring(0,postion) + s.substring(postion + length, Length);
        return newString;//返回已经删除好的字符串
    }

    public static String sHA1(Context context) {
        try {
            PackageInfo info = context.getPackageManager().getPackageInfo(context.getPackageName(), PackageManager.GET_SIGNATURES);
            byte[] cert = info.signatures[0].toByteArray();
            MessageDigest md = MessageDigest.getInstance("SHA1");
            byte[] publicKey = md.digest(cert);
            StringBuffer hexString = new StringBuffer();
            for (int i = 0; i < publicKey.length; i++) {
                String appendString = Integer.toHexString(0xFF & publicKey[i])
                        .toUpperCase(Locale.US);
                if (appendString.length() == 1)
                    hexString.append("0");
                hexString.append(appendString);
                hexString.append(":");
            }
            String result = hexString.toString();
            return result.substring(0, result.length()-1);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }
    public static void show(String from,String str) {
        str = str.trim();
        int index = 0;
        int maxLength = 8000;
        String sub;
        while (index < str.length()) {
            // java的字符不允许指定超过总的长度end
            if (str.length() <= index + maxLength) {
                sub = str.substring(index);
            } else {
                sub = str.substring(index, maxLength);
            }

            index += maxLength;
            Log.i(from, sub.trim());
        }
    }


}
